package com.mangatta.cloud.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.mangatta.cloud.Metric;
import com.mangatta.cloud.Statistic;
import com.mangatta.cloud.alarm.AlarmConfiguration;

import java.util.ArrayList;
import java.util.List;

public class AlarmConfigDao extends DbHelper {
    private static final String TAG = AlarmConfigDao.class.getSimpleName();

    public AlarmConfigDao(Context context) {
        super(context);
    }

    public List<AlarmConfiguration> getAlarmConfigsForInstance(String instanceId) {
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.query(AlarmConfigurationTable.TABLE_ALARM_CONFIGURATIONS, null, AlarmConfigurationTable.C_INSTANCE + "=?",
                new String[]{instanceId}, null, null, AlarmConfigurationTable.C_METRIC + " asc");
        if (cursor == null) {
            db.close();
            return null;
        }
        List<AlarmConfiguration> alarmConfigurations = new RowBeanMapper(cursor).getList();
        cursor.close();
        db.close();
        return alarmConfigurations;
    }

    public void deleteAlarmConfigsForInstance(String instanceId) {
        SQLiteDatabase db = getWritableDatabase();
        int delete = db.delete(AlarmConfigurationTable.TABLE_ALARM_CONFIGURATIONS, AlarmConfigurationTable.C_INSTANCE + "=?",
                new String[]{instanceId});
        Log.d(TAG, "No.of alarm config rows deleted for instance" + instanceId + " is " + delete);
        db.close();
    }

    class RowBeanMapper {
        Cursor cursor;

        public RowBeanMapper(Cursor cursor) {
            this.cursor = cursor;
        }

        public List<AlarmConfiguration> getList() {
            List<AlarmConfiguration> configList = new ArrayList<AlarmConfiguration>();

            while (cursor.moveToNext()) {
                String instanceId = cursor.getString(cursor.getColumnIndex(AlarmConfigurationTable.C_INSTANCE));
                String metric = cursor.getString(cursor.getColumnIndex(AlarmConfigurationTable.C_METRIC));
                String statistic = cursor.getString(cursor.getColumnIndex(AlarmConfigurationTable.C_STATISTIC));
                Double threshold = cursor.getDouble(cursor.getColumnIndex(AlarmConfigurationTable.C_THRESHOLD));

                AlarmConfiguration alarmConfig = new AlarmConfiguration(instanceId,
                        Metric.valueOf(metric),
                        Statistic.valueOf(statistic),
                        threshold == null ? null : threshold);
                configList.add(alarmConfig);
            }
            cursor.close();
            Log.d(TAG, "Fetched rows of size" + configList.size());
            return configList;
        }
    }

    public void saveAlarmConfig(List<AlarmConfiguration> configList) {
        if (configList == null || configList.isEmpty()) {
            return;
        }
        deleteAlarmConfigsForInstance(configList.get(0).getInstance());
        SQLiteDatabase writableDatabase = getWritableDatabase();
        for (AlarmConfiguration config : configList) {
            ContentValues values = new ContentValues();
            values.put(AlarmConfigurationTable.C_THRESHOLD, config.getThreshold() == null ?
                    null : Double.valueOf(config.getThreshold().toString()));
            values.put(AlarmConfigurationTable.C_INSTANCE, config.getInstance());
            values.put(AlarmConfigurationTable.C_STATISTIC, config.getStatistic().name());
            values.put(AlarmConfigurationTable.C_METRIC, config.getMetric().name());

            try {
                writableDatabase.insertOrThrow(AlarmConfigurationTable.TABLE_ALARM_CONFIGURATIONS, null, values);
            } catch (SQLException e) {
                Log.e(TAG, "Instance cant be persisted", e);
            }
        }
        writableDatabase.close();
    }
}
